﻿using System;
using System.Collections.Generic;
using System.Configuration;
using LocalTest.Domain.Entities;
using LocalTest.Domain.Shared;
using LocalTest.Infrastructure.Conventions;
using LocalTest.Utils;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace LocalTest.Infrastructure.Context;

public partial class AppDbContext : DbContext
{
    public AppDbContext()
    {
    }

    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<User> Users { get; private set; }
    
    public virtual DbSet<Designer> Designers { get; private set; }

    public virtual DbSet<Goods> Goods { get; private set; }

    public virtual DbSet<UserLayer> UserLayers { get; private set; }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .LogTo(Console.WriteLine, LogLevel.Information)
            .UseMySql(AppSettings.Configuration.GetConnectionString("MySqlConnectionString") ?? "",
                ServerVersion.Parse("8.0.28-mysql"))
            .UseSnakeCaseNamingConvention();
    }

    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {
        configurationBuilder.Conventions.Add(_ => new StringLengthConvention());
        base.ConfigureConventions(configurationBuilder);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
        base.OnModelCreating(modelBuilder);
    }
}